<?php

namespace App\Http\Controllers\Api\V3;

use App\Http\Controllers\Controller;
use App\Models\V1\AppUserMessageModel;
use App\Models\V1\MemberModel;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class AppUseMessageController extends Controller
{
    use Helpers;

    /**
     * @var AppUserMessageModel
     */
    private $appUserMessageModel;

    public function __construct(AppUserMessageModel $appUserMessageModel)
    {
        $this->appUserMessageModel = $appUserMessageModel;
    }

    /**
     * 保存APP的使用的情况
     * @param Request $request
     * @return mixed
     */
    public function use_device_info(Request $request)
    {
        $mobile      = $request->mobile;
        $key         = $request->key;
        $openAppDate = $request->openAppDate;//用户打开app的时间
        $phoneSystem = $request->phoneSystem;//用户的手机的系统版本信息
        $appVersion  = $request->appVersion;//app的版本信息
        $status      = $this->check_user($request->all());
        $message     = isset($status) ? $status->messages() : '';
        if ($status->fails()) {
            return $this->response->array(['code' => "403", 'message' => '参数错误', 'data' => []]);
        }
        $memberinfo = new MemberModel();
        $userInfo   = $memberinfo->get_member_info_by_mobile_key($mobile, $key);//用户提交用户的信息
        if (!$userInfo) {
            return $this->response->array(['code' => '404', 'message' => '用户信息不存在', 'data' => []]);
        }
        $userId = $userInfo->UserID;

        $status  = $this->appUserMessageModel->store($userId, $openAppDate, $phoneSystem, $appVersion);//保存用户的信息
        $message = isset($status) ? '保存成功' : '保存失败';
        return $this->response->array(['code' => '200', 'message' => $message, 'data' => []]);
    }


    public function check_user($parm)
    {
        $data = Validator::make($parm,
            [
                'mobile'      => 'required',
                'key'         => 'required',
                'openAppDate' => 'required',
                'phoneSystem' => 'required',
                'appVersion'  => 'required'
            ],
            [
                'mobile.required'      => '手机号不能为空',
                'key.required'         => '设备号不能为空',
                'openAppDate.required' => '用户开启app的时间不能为空',
                'phoneSystem.required' => '手机的系统不能为空',
                'appVersion.required'  => 'app的版本号不能为空'
            ]
        );

        return $data;
    }
}